import Vue from 'vue';
//导入数据操作API
import api from "@/service/api";
import http from "@/service/http";

//状态
const state={
	classifyId:'',
	//列表state
	assetInfos:[], 
	//表格选择行数据
	selects:[], 
	assetInfoItem:{
		id:'',
		brand:"",
		assetName:'',
		unit:'',
		unitPrice:'',
		standrad:'',
		classifyId:'',
		classifyName:''
	},
	//总记录数
	total:0, 
	//当前页
	currentPage:1, 
	//每页显示记录数
	pageSize:10, 
	//表格加载状态
	listLoading:false, 
	//过滤条件
	filters:{
		employeename:'',
		employeesex:'',
		created_at:'',
		status:''
	},
	selectType:1,//是否来自选择,1:默认多选，2：单选
	ableTag:'1',//可用 
	isDialogShow:false
};

//getter
const getters={
	classifyId:state=>state.classifyId,
	assetInfos:state=>state.assetInfos,
	selects:state=>state.selects,
	total:state=>state.total,
	currentPage:state=>state.currentPage,
	pageSize:state=>state.pageSize,
	listLoading:state=>state.listLoading,
	filters:state=>state.filters,
	selectType:state=>state.selectType,
	selectItem:state=>state.selectItem,
	isDialogShow:state=>state.isDialogShow, 
	ableTag:state=>state.ableTag,
	assetInfoItem:state=>state.assetInfoItem
};

//action
const actions={
	//查询列表
	getInfos({commit,state},keyNameStr){
		let param={
			classifyId:state.classifyId,
			pageNum:state.currentPage,
			pageSize:state.pageSize,
			status:state.ableTag,
			keyName:keyNameStr
		};
		commit('listLoading',true);
		http.post(api.sysApi.searchAssetsInfo,param).then(data => {
			if(!data.success){
				return;
			}
            commit('loadList',data);
			commit('listLoading',false);
        }); 
	}, 
	//表格选择行
	selectsChange({commit,dispatch},selects){
		commit('selectsChange',selects);
	}, 
	//item选择
	selectItemChange({commit,dispatch},assetInfoItem){
		commit('setAssetInfoItem',assetInfoItem);
	}, 
	//表格翻页
	currentChange({commit,dispatch},currentPage){
		commit('currentChange',currentPage);
		dispatch('getInfos');
	},
	//表格设置每页条数
	sizeChange({commit,dispatch},pageSize){
		commit('sizeChange',pageSize);
		dispatch('getInfos');
	},
	//重置过虑器
	resetFilters({commit,dispatch},filters){
		commit('resetFilters',filters);
		dispatch('getInfos');
	},
	//表格载入状态
	listLoading({commit},isShow){
		commit('listLoading',isShow);
	},
	//是否来自选择
	toSelect({commit},selectType){
		commit('isSelectUser',selectType);
	},
	//是否显示为dialog
	showDialog({commit},isDialogShow){
		commit('setDialogShow',isDialogShow);
	},
	//选中单个
	selectInfoItem({commit},selectItem){
		commit('setSelectItem',selectItem);
	},
	setClassifyId({commit,dispatch},classifyId){
		commit('setClassifyId',classifyId);
		dispatch('getInfos');
	}
};

//mutations
const mutations={loadList(state,param){
		state.total=param.totalNum;
		state.assetInfos=param.data;
	},
	setClassifyId(state,classifyId){
		state.classifyId=classifyId;
	},
	//表格载入状态变更
	listLoading(state,isShow){
		state.listLoading=isShow;
	},
	//表格选中行数据状态变更
	selectsChange(state,selects){
		if(selects.length==0){
			state.selects=[];
		}else{
			state.selects=selects;
		}
	},
	//表格翻页状态变更
	currentChange(state,currentPage){
		state.currentPage=currentPage;
	}, 
	//表格每页显示条数状态变更
	sizeChange(state,pageSize){
		state.pageSize=pageSize;
	},
	//查询条件过虑器状态充更
	resetFilters(state,filters){
		state.filters=filters;
	},
	//是否来自选择用户
	isSelectUser(state,selectType){
		state.selectType=selectType;
		state.ableTag = 1;
	},
	setSelectItem(state,infoItem){
		state.infoItem = infoItem;
	},
	setDialogShow(state,isDialogShow){
		state.isDialogShow=isDialogShow;
	},
	setAssetInfoItem(state,assetInfoItem){
		state.assetInfoItem=assetInfoItem;
	}
};

//导出
export default{
	//启用命名空间
	namespaced:true,
	state,
	getters,
	actions,
	mutations
};